package com.ihr360.payroll.service.attendanceplan.impl;

import com.ihr360.payroll.dao.EnrollAdjustRuleRepository;
import com.ihr360.payroll.model.dto.attendance.rule.enroll.EnrollAdjustRule;
import com.ihr360.payroll.service.attendanceplan.EnrollAdjustRuleService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tomtao
 */
@Service
public class EnrollAdjustRuleServiceImpl extends AbstractSimpleRestServiceImpl<EnrollAdjustRule, Long> implements EnrollAdjustRuleService {

    @Autowired
    EnrollAdjustRuleRepository enrollAdjustRuleRepository;

    @Override
    public SimpleRestReposity<EnrollAdjustRule, Long> getSimpleRestReposity() {
        return enrollAdjustRuleRepository;
    }

    @Override
    public EnrollAdjustRule save(EnrollAdjustRule enrollAdjustRule) {
        if(enrollAdjustRule.getId() != null) {
            EnrollAdjustRule ret = enrollAdjustRuleRepository.findOne(enrollAdjustRule.getId());
            if(ret == null) {
                return null;
            }
            ret.setFormula(enrollAdjustRule.getFormula());
            ret.setBasementType(enrollAdjustRule.getBasementType());
            ret.setBaseRatio(enrollAdjustRule.getBaseRatio());
            ret.setFixedBase(enrollAdjustRule.getFixedBase());
            ret.setNumOpRatio(enrollAdjustRule.getNumOpRatio());
            ret.setReferenceFields(enrollAdjustRule.getReferenceFields());
            ret.setRemark(enrollAdjustRule.getRemark());
            ret.setUpdatedAt(new Date());
            return enrollAdjustRuleRepository.save(ret);
        }
        return enrollAdjustRuleRepository.save(enrollAdjustRule);
    }
}
